package com.yangjie.web.jsf.login;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.yangjie.common.service.Languages;
import com.yangjie.common.service.MessageProvider;

@Component
@ManagedBean(name="loginBean")
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class LoginBean {
	Logger logger = Logger.getLogger(this.getClass());
	private String name = null;
	private String password = null;
	private String language = null;
	
	@Inject
	MessageProvider messageProvider = null;
	
	@Inject
	Languages languages = null;
	
	public LoginBean() {		
		logger.debug("init locale:" + FacesContext.getCurrentInstance().getViewRoot().getLocale() + ":" + FacesContext.getCurrentInstance().getViewRoot());
		Map<String, Object> requestCookieMap = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
		Cookie preLocale = (Cookie)requestCookieMap.get("locale");
		if(preLocale != null) {
			FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(preLocale.getValue()));
		}
	}
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public List<String> getLanguageList() {
		return languages.getSupportedLanguages();
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void logout() {

	    //do any job with the associated values that you've got from the user, like persisting attempted login, etc.
	    FacesContext facesContext = FacesContext.getCurrentInstance();
	    ExternalContext extenalContext = facesContext.getExternalContext();
	    RequestDispatcher dispatcher = ((ServletRequest)extenalContext.getRequest()).getRequestDispatcher("/j_spring_security_logout");
	    try {
			dispatcher.forward((ServletRequest)extenalContext.getRequest(), (ServletResponse)extenalContext.getResponse());
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    facesContext.responseComplete();
	
	}
	public String login() {
	    //do any job with the associated values that you've got from the user, like persisting attempted login, etc.
	    FacesContext facesContext = FacesContext.getCurrentInstance();
	    Locale locale = null;
	    if(language == null) {
	    	language = languages.getDefaultLanguage();

	    } 	    	
	    locale = new Locale(language);
    	facesContext.getViewRoot().setLocale(locale);
    	messageProvider.setLocale(locale);
    	facesContext.getExternalContext().addResponseCookie("locale", language, null);
	    logger.debug("locale is " + locale + ":" + facesContext.getViewRoot());
	    
	    ExternalContext extenalContext = facesContext.getExternalContext();
	    RequestDispatcher dispatcher = ((ServletRequest)extenalContext.getRequest()).getRequestDispatcher("/j_spring_security_check");
	    try {
			dispatcher.forward((ServletRequest)extenalContext.getRequest(), (ServletResponse)extenalContext.getResponse());
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "login_failure";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "login_failure";
		}
	    facesContext.responseComplete();
	    return null;
	}
	

}
